package com.bbcare.encrypt;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class EncryptUtil {

    public static Properties properties = new Properties();
    static {
        InputStream inputStream = EncryptUtil.class.getClassLoader()
                .getResourceAsStream("encrypt.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperties(String name) {
        return properties.getProperty(name);
    }

    /**
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     *
     * @throws //NoSuchAlgorithmException
     * @throws //UnsupportedEncodingException
     */
    public static String getAESKey() throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128);
        SecretKey sk = kg.generateKey();
        byte[] b = sk.getEncoded();
        return parseByte2HexStr(b);
    }

    // 类型转换，去掉unchecked cast warning
    @SuppressWarnings("unchecked")
    public static <T> T typeCast(Object obj) {
        return (T) obj;
    }

    public static String dealWith(String obj, String encryptionMethod)
            throws Exception {
        // 判断是否传入加密方式
        if (null == encryptionMethod || "".equals(encryptionMethod)) {
            int length = obj.toString().length();
            if (length >= 3) {
                String str = obj.toString().substring(length - 3, length);
                if (EncryptUtil.getProperties("ENCRYPT_001").equals(str)) {
                    encryptionMethod = EncryptConstants.ENCRYPT_AES;
                } else if (EncryptUtil.getProperties("ENCRYPT_002").equals(str)) {
                    encryptionMethod = EncryptConstants.ENCRYPT_3DES;
                } else {
                    throw new RuntimeException("密文不完整！");
                }
            } else {
                throw new RuntimeException("密文不完整！");
            }
        }
        return encryptionMethod;
    }
}
